package com.zhuoya.healthcard;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.AppUtils;
import com.epson.isv.eprinterdriver.Common.EpsPrinter;
import com.epson.isv.eprinterdriver.Ctrl.EPrintManager;
import com.epson.isv.eprinterdriver.Ctrl.ISearchPrinterListener;
import com.zhuoya.healthcard.contants.Contants;
import com.zhuoya.healthcard.entity.FaceEnvironment;
import com.zhuoya.healthcard.entity.FaceStatusEnum;
import com.zhuoya.healthcard.update.AppUpdate;
import com.zhuoya.healthcard.util.HttpUtil;

import org.apache.commons.lang3.StringUtils;


public class MainActivity extends BaseActity implements ISearchPrinterListener {
    final int ACT_FOR_REQUEST_PERMISSION;
    final int ACT_FOR_RESULT_IMAGE;
    final int ACT_FOR_RESULT_SEARCH;
    EPrintManager epManager;
    private ImageView mAppoint;
    private ImageView mDelivery;
    private ImageView mNoAppont;

    static {
        System.loadLibrary("jpeg-9b");
    }

    public MainActivity() {
        this.ACT_FOR_RESULT_IMAGE = 1;
        this.ACT_FOR_RESULT_SEARCH = 2;
        this.ACT_FOR_REQUEST_PERMISSION = 10;
    }

    private void initView() {
        AppUpdate.getInstance().checkUpdate();
        TextView version = findViewById(R.id.app_version);
        version.setText("版本号："+AppUtils.getAppVersionName());

        (this.mAppoint = this.findViewById(R.id.appoint)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { // 线上登记
                final Intent intent = new Intent((Context) MainActivity.this, ScanCardActivity.class);
                intent.putExtra("pageStatus", 1);
                MainActivity.this.startActivity(intent);
                MainActivity.this.finish();
            }
        });
        (this.mNoAppont = this.findViewById(R.id.no_appont)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) { // 线下叫号
                final Intent intent = new Intent((Context) MainActivity.this, ScanCardActivity.class);
                intent.putExtra("pageStatus", 2);
                MainActivity.this.startActivity(intent);
                MainActivity.this.finish();
            }
        });
        (this.mDelivery = this.findViewById(R.id.delivery)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) { // 寄送
                final Intent intent = new Intent((Context) MainActivity.this, ScanCardActivity.class);
                intent.putExtra("pageStatus", 3);
                MainActivity.this.startActivity(intent);
                MainActivity.this.finish();
            }
        });
        // 初始化获取医院信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final JSONObject jsonObject = new JSONObject();
                    jsonObject.put("hospitalId", Contants.HOSPITALID);
                    final JSONObject jsonObject2 = JSON.parseObject(HttpUtil.submitPostData(Contants.uri + "hospital/selectById", jsonObject, "utf-8")).getJSONObject("hospital");
                    if (jsonObject2 != null) {
                        if (StringUtils.isNotEmpty(jsonObject2.getString("name"))) {
                            Contants.HOSPITALNAME = jsonObject2.getString("name");
                        }
                        if (StringUtils.isNotEmpty(jsonObject2.getString("phone"))) {
                            Contants.HOSPITALPHONE = jsonObject2.getString("phone");
                        }
                        if (StringUtils.isNotEmpty(jsonObject2.getString("address"))) {
                            Contants.HOSPITALADDRESS = jsonObject2.getString("address");
                        }
                        if (StringUtils.isNotEmpty(jsonObject2.getString("week"))) {
                            Contants.HOSPITALWEEK = jsonObject2.getString("week");
                        }
                        Contants.HOSPITALAM = jsonObject2.getString("am");
                        Contants.HOSPITALPM = jsonObject2.getString("pm");
                    }
                } catch (Exception ex) {
                }
            }
        }).start();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void checkPermission() {
        if (this.checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED && this.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (this.shouldShowRequestPermissionRationale("android.permission.READ_EXTERNAL_STORAGE") && this.shouldShowRequestPermissionRationale("android.permission.WRITE_EXTERNAL_STORAGE")) {
            this.requestPermissions(new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"}, 10);
            return;
        }
        this.requestPermissions(new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"}, 10);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 23) {
            this.checkPermission();
        }
        (this.epManager = EPrintManager.instance()).addSearchListener(this);
        this.epManager.findPrinter(30);
        this.initView();
        FaceEnvironment.setSoundId(FaceStatusEnum.Idcard_Identify, R.raw.idcrad_identify);
    }

    @Override
    public void onFindPrinter(final EpsPrinter epsPrinter) {
        Contants.epsPrinter = epsPrinter;
        this.epManager.cancelFindPrinter();
    }

    @Override
    public void onSearchBegin() {
    }

    @Override
    public void onSearchFinished(final int n) {
    }
}
